Coverage Report

Created: 2026-02-05 09:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\scloud-dns\scloud-dns\src\dns\q_class.rs
Line
Count
Source
1
use crate::exceptions::SCloudException;
2
3
/// DNS CLASS field (QCLASS / CLASS) as defined in DNS RFCs.
4
///
5
/// This enum represents the DNS class of a resource record or query.
6
/// The most common class is `IN` (Internet).
7
///
8
/// Supported classes:
9
/// - IN   (1): Internet
10
/// - CS   (2): CSNET (obsolete)
11
/// - CH   (3): CHAOS
12
/// - HS   (4): Hesiod
13
/// - NONE (0): Used in dynamic update (RFC 2136)
14
/// - ANY  (255): Wildcard class (RFC 1035)
15
///
16
/// The enum supports conversion to and from the on-the-wire `u16`
17
/// representation used in DNS packets.
18
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
19
pub(crate) enum DNSClass {
20
    IN,
21
    CS,
22
    CH,
23
    HS,
24
    NONE,
25
    ANY,
26
}
27
28
impl TryFrom<u16> for DNSClass {
29
    type Error = SCloudException;
30
31
    /// Convert a raw DNS CLASS value (`u16`) into a `DNSClass`.
32
    ///
33
    /// # Errors
34
    /// Returns `SCLOUD_QCLASS_U16_FOR_DNSCLASS_UNKNOWN` if the value
35
    /// does not match any known DNS class.
36
17
    fn try_from(v: u16) -> Result<DNSClass, Self::Error> {
37
17
        match v {
38
1
            0 => Ok(DNSClass::NONE),
39
10
            1 => Ok(DNSClass::IN),
40
1
            2 => Ok(DNSClass::CS),
41
2
            3 => Ok(DNSClass::CH),
42
1
            4 => Ok(DNSClass::HS),
43
2
            255 => Ok(DNSClass::ANY),
44
0
            _ => Err(SCloudException::SCLOUD_QCLASS_U16_FOR_DNSCLASS_UNKNOWN),
45
        }
46
17
    }
47
}
48
49
impl TryFrom<DNSClass> for u16 {
50
    type Error = SCloudException;
51
52
    /// Convert a `DNSClass` into its DNS wire format (`u16`).
53
    ///
54
    /// # Errors
55
    /// This error should never occur unless an invalid enum variant
56
    /// is introduced.
57
11
    fn try_from(c: DNSClass) -> Result<u16, Self::Error> {
58
        #[allow(unreachable_patterns)]
59
11
        match c {
60
1
            DNSClass::NONE => Ok(0),
61
6
            DNSClass::IN => Ok(1),
62
1
            DNSClass::CS => Ok(2),
63
1
            DNSClass::CH => Ok(3),
64
1
            DNSClass::HS => Ok(4),
65
1
            DNSClass::ANY => Ok(255),
66
0
            _ => Err(SCloudException::SCLOUD_QCLASS_DNSCLASS_FOR_U16_UNKNOWN),
67
        }
68
11
    }
69
}
70
71
impl From<&[u8; 2]> for DNSClass {
72
    /// Convert a 2-byte DNS wire representation into a `DNSClass`.
73
    ///
74
    /// # Panics
75
    /// Panics if the value does not correspond to a known DNS class.
76
3
    fn from(bytes: &[u8; 2]) -> Self {
77
3
        let v = u16::from_be_bytes(*bytes);
78
3
        DNSClass::try_from(v).unwrap()
79
3
    }
80
}